package qsc.algorithm.dijkstra;

import java.util.Arrays;

/**
 * @auther QiuShangcheng
 * @create 2021/5/3
 */
public class Review {
    public static void main(String[] args) {
        char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
        // 邻接矩阵
        int[][] matrix = new int[vertex.length][vertex.length];
        final int N = 65535;// 表示不可以连接
        matrix[0] = new int[] { N, 5, 7, N, N, N, 2 };
        matrix[1] = new int[] { 5, N, N, 9, N, N, 3 };
        matrix[2] = new int[] { 7, N, N, N, 8, N, N };
        matrix[3] = new int[] { N, 9, N, N, N, 4, N };
        matrix[4] = new int[] { N, N, 8, N, N, 5, 4 };
        matrix[5] = new int[] { N, N, N, 4, 5, N, 6 };
        matrix[6] = new int[] { 2, 3, N, N, 4, 6, N };

        Graph graph = new Graph(vertex, matrix, vertex.length - 1);
        graph.show();
        long l = System.currentTimeMillis();
        review(graph,6);
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }

    public static void review(Graph graph, int begin) {
        int[] dis = new int[graph.getVertex().length];
        int[] prev = new int[graph.getVertex().length];
        boolean[] visited = new boolean[graph.getVertex().length];
        for (int i = 0; i < graph.getVertex().length; i++) {
            dis[i] = graph.getMatrix()[begin][i];
        }
        visited[begin] = true;
        dis[begin] = 0;
        prev[begin] = begin;

        int minPath;
        int temp = begin;
        for (int i = 0; i < graph.getVertex().length; i++) {
            minPath = 65535;
            for (int j = 0; j < graph.getVertex().length; j++) {
                if (dis[j]<minPath && visited[j] == false){
                    minPath = dis[j];
                    temp = j;
                }
            }
            visited[temp] = true;

            for (int j = 0; j < graph.getVertex().length; j++) {
                int tempPath = (graph.getMatrix()[temp][j] == 65535) ? 65535 : minPath + graph.getMatrix()[temp][j];
                if (tempPath < dis[j] && visited[j] == false){
                    dis[j] = tempPath;
                }
            }
        }
        System.out.println("dist[]: "+Arrays.toString(dis));
        for (int j = 0; j < graph.getVertex().length; j++) {
            System.out.printf("dijkstra(%c): ", graph.getVertex()[j]);
            System.out.printf("  shortest(%c, %c)=%d\n", graph.getVertex()[begin], graph.getVertex()[j], dis[j]);
        }
    }
}
